package Offer.offer2022;

/**
 * 替换空格
 */
public class Test04 {
    public static void main(String[] args) {
        String originStr = replaceBlank("we are happy");
        System.out.println(originStr);
    }

    public static String replaceBlank(String originStr) {
        if (originStr == null || originStr.length() == 0) {
            return null;
        }

        char[] charArray = originStr.toCharArray();
        int blankCount = 0;
        for (int i = 0; i < charArray.length; i++) {
            if (charArray[i] == ' ') {
                blankCount++;
            }
        }
        char[] newCharArray = new char[charArray.length + 2 * blankCount];

        int j = 0;
        for (int i = 0; i < charArray.length; i++) {
            if (charArray[i] == ' ') {
                newCharArray[j++] = '2';
                newCharArray[j++] = '0';
                newCharArray[j++] = '%';
            } else {
                newCharArray[j++] = charArray[i];
            }
        }
        return new String(newCharArray);
    }
}
